<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Training extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
    
    
    public function send(){
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://wq.hoeagri.com/api/Training/receiveInput?x=133&y=187&score=20&timestamp=1734357062&retry=0&sign=2f99e414981482ef9f2a9d57eb04cec1');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'en-US,en;q=0.8',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Connection' => 'keep-alive',
        ]);
        
        $response = curl_exec($ch);
        
        curl_close($ch);
    }

    /**
     * 首页
     *
     */
    public function receiveInput()
    {
        // 获取 PHP 原始输入流
        $rawInput = file_get_contents("php://input");
        
        // 将 JSON 解码为 PHP 数组
        $data = json_decode($rawInput, true);
        
        //  $data = array (
        //               'x' => 2.3,
        //               'y' => 1.6,
        //               'score' => 30,
        //               'timestamp' => 1732364302,
        //               'retry' => 0,
        //               'sign' => 'a766ad89f923fe50b2a8651b0c1b0284',
        //         );

        // 检查解码是否成功
        // if (json_last_error() === JSON_ERROR_NONE) {
            
            // 获取当前日期（格式：YYYY-MM-DD）
                $date = date('Y-m-d');
                
                // 定义文件路径，包含日期
                $filename = __DIR__ . "/log/log_{$date}.txt";
                
                // echo $filename;
                
                // 要写入的内容
                $content = "这是一些日志内容，记录于 " . date('Y-m-d H:i:s') . PHP_EOL;
                
                // 使用 file_put_contents 写入文件，追加模式
                $result = file_put_contents($filename, var_export($data,true), FILE_APPEND);
                
               
                
                
                //入库
                unset($data['sign']);
                
                $result = Db::name('xyvenue_video')->insert($data);
                
                if($result){
                    // 成功解析 JSON 数据
                     return json(['status' => 'success']);
                     
                }else{
                    
                    return json(['status' => 'error', 'message' => 'Invalid JSON']);
                }
                
            
        // } else {
        //     // JSON 数据解析失败
        //     return json(['status' => 'error', 'message' => 'Invalid JSON']);
        // }
    }
}
